package com.lufei.dh.ShoppingCart;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.animation.TypeEvaluator;
import android.app.Activity;
import android.graphics.PointF;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lufei.animain.CurtainView;
import com.lufei.dh.R;
import com.lufei.dh.ShoppingCart.Adapter.Adapter_Card;
import com.lufei.dh.ShoppingCart.Bean.Bean_Item;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/3/24 0024.
 */

public class Card_Activity extends Activity implements  Animator.AnimatorListener{
    @BindView(R.id.id_recyclerview)
    RecyclerView idRecyclerview;
    @BindView(R.id.bt_main_mode)
    Button btMainMode;
    @BindView(R.id.cb_main_checkAll)
    CheckBox cbMainCheckAll;
    @BindView(R.id.tv_main_count)
    TextView tvMainCount;
    @BindView(R.id.bt_main_pay)
    Button btMainPay;
    @BindView(R.id.id_curtain)
    CurtainView idCurtain;
    @BindView(R.id.id_ralativelayout)
    RelativeLayout idRalativelayout;
    private Adapter_Card adapterCard;
    private List<Bean_Item> data = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.card_activity);
        ButterKnife.bind(this);
        setIdRecyclerview();
        setData();
        setAdapterCard();


        idCurtain.setMaxLength(300);
        idCurtain.setRecoveryTime(300);
    }

    public void setIdRecyclerview() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        idRecyclerview.setLayoutManager(layoutManager);
        adapterCard = new Adapter_Card(data, this);
        idRecyclerview.setAdapter(adapterCard);
    }

    private void setData() {
        for (int i = 0; i < 40; i++) {
            data.add(new Bean_Item(i + "号" + "商品", 2, 0, false));
        }
        adapterCard.notifyDataSetChanged();
    }

    private void setAdapterCard() {
        adapterCard.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
//        R.id.bt_item_delete
                List<Bean_Item> mData = adapter.getData();
                Bean_Item bean_item = mData.get(position);
                switch (view.getId()) {
                    case R.id.bt_item_delete:

//                        adapterCard.remove(position);
//                        adapterCard.replaceData(data);
                        if (bean_item.isSelected()) {
                            setTvMainCount(-bean_item.getMoney() * bean_item.getCount());
                        }
                        adapter.notifyItemRemoved(position);
                        adapter.getData().remove(position);

                        Log.e("xcvxh", adapter.getData() + "\n" + Card_Activity.this.data);
                        break;
                    case R.id.bt_item_add:

                        Log.e("xcvxh555", bean_item.isSelected() + "");
                        bean_item.setCount(bean_item.getCount() + 1);

                        TextView viewByPosition = (TextView) adapter.getViewByPosition(idRecyclerview, position, R.id.tv_item_count);
                        viewByPosition.setText(String.valueOf(bean_item.getCount()));
//                        adapter.notifyItemChanged(position);
                        Log.e("xcvxh555", bean_item.isSelected() + "");
                        if (bean_item.isSelected()) {
                            add(view);
                            setTvMainCount(bean_item.getMoney());
                        }

                        break;
                    case R.id.bt_item_subtract:
                        if (bean_item.getCount() != 0) {
                            bean_item.setCount(bean_item.getCount() - 1);
                            TextView viewByPosition2 = (TextView) adapter.getViewByPosition(idRecyclerview, position, R.id.tv_item_count);
                            viewByPosition2.setText(String.valueOf(bean_item.getCount()));
//                            adapter.notifyItemChanged(position);
                            if (bean_item.isSelected()) {
                                setTvMainCount(-bean_item.getMoney());
                            }
                        } else {
                            Toast.makeText(Card_Activity.this, "商品数量为0", Toast.LENGTH_SHORT).show();
                        }

                        break;
                    case R.id.cb_item_check:
                        Log.e("xcvxh1", bean_item.isSelected() + "");

                        if (bean_item.isSelected()) {
                            setTvMainCount(-bean_item.getCount() * bean_item.getMoney());
                            bean_item.setSelected(false);
                            setCheckAll();
                            Log.e("xcvxh555", bean_item.isSelected() + "");
                        } else {

                            setTvMainCount(bean_item.getCount() * bean_item.getMoney());
                            bean_item.setSelected(true);
                            Log.e("xcvxh00", bean_item.isSelected() + "");
                            isCheckAll();
                        }
                        Log.e("xcvxh2", bean_item.isSelected() + "");


                        break;

                }
            }
        });
    }

    private boolean xc = true;

    @OnClick({R.id.bt_main_mode, R.id.id_recyclerview, R.id.bt_main_pay, R.id.cb_main_checkAll})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bt_main_mode:
                if (xc) {
                    xc = false;
                    adapterCard.setmButton(true);
                } else {
                    xc = true;
                    adapterCard.setmButton(false);
                }

                break;
            case R.id.id_recyclerview:
                break;
            case R.id.bt_main_pay:
                List<String> list = new ArrayList<>();
                for (int i = 0; i < adapterCard.getData().size(); i++) {
                    if (adapterCard.getData().get(i).isSelected()) {
                        list.add(adapterCard.getData().get(i).getName());
                    }
                }
                Log.e("xcvxhzzz", list.toString());
                break;
            case R.id.cb_main_checkAll:
                if (cbMainCheckAll.isChecked()) {
                    setCbMainCheckAll(true);
                } else {
                    setCbMainCheckAll(false);
                }
                break;
        }
    }

    private int mCount = 0;

    private void setTvMainCount(int count) {
        mCount = mCount + count;
        tvMainCount.setText(String.valueOf(mCount));
    }

    private void setCbMainCheckAll(boolean checkAll) {
        int count = 0;
        if (checkAll) {
            List<Bean_Item> data = adapterCard.getData();
            for (int i = 0; i < data.size(); i++) {
                data.get(i).setSelected(true);
                count = count + data.get(i).getCount() * data.get(i).getMoney();
            }
            adapterCard.notifyDataSetChanged();

            tvMainCount.setText(String.valueOf(count));
            mCount = count;
        } else {
            List<Bean_Item> data = adapterCard.getData();
            for (int i = 0; i < data.size(); i++) {
                data.get(i).setSelected(false);
                count = count + data.get(i).getCount() * data.get(i).getMoney();
            }
            adapterCard.notifyDataSetChanged();
            tvMainCount.setText(String.valueOf(0));
            mCount = 0;
        }
    }

    private void isCheckAll() {
        int count = 0;
        Log.e("xcvxc", count + "_____");
        List<Bean_Item> data = adapterCard.getData();
        for (int i = 0; i < data.size(); i++) {
            if (data.get(i).isSelected()) {
                count++;
            }
        }
        if (count == data.size()) {
            Log.e("xcvxc", count + "");
            cbMainCheckAll.setChecked(true);
        }

    }

    private void setCheckAll() {
        if (cbMainCheckAll.isChecked()) {
            cbMainCheckAll.setChecked(false);
        }
    }

    private void setanim() {

    }

    /**
     * ListView + 点击回调方法
     */
    public void add(View addV) {
        int[] childCoordinate = new int[2];
        int[] parentCoordinate = new int[2];
        int[] shopCoordinate = new int[2];
        //1.分别获取被点击View、父布局、购物车在屏幕上的坐标xy。
        addV.getLocationInWindow(childCoordinate);
        idRalativelayout.getLocationInWindow(parentCoordinate);
        cbMainCheckAll.getLocationInWindow(shopCoordinate);

        //2.自定义ImageView 继承ImageView
        MoveImageView img = new MoveImageView(this);
        img.setImageResource(R.mipmap.ic_launcher);
        //3.设置img在父布局中的坐标位置
        img.setX(childCoordinate[0] - parentCoordinate[0]);
        img.setY(childCoordinate[1] - parentCoordinate[1]);
        //4.父布局添加该Img
        idRalativelayout.addView(img);

        //5.利用 二次贝塞尔曲线 需首先计算出 MoveImageView的2个数据点和一个控制点
        PointF startP = new PointF();
        PointF endP = new PointF();
        PointF controlP = new PointF();
        //开始的数据点坐标就是 addV的坐标
        startP.x = childCoordinate[0] - parentCoordinate[0];
        startP.y = childCoordinate[1] - parentCoordinate[1];
        //结束的数据点坐标就是 shopImg的坐标
        endP.x = shopCoordinate[0] - parentCoordinate[0];
        endP.y = shopCoordinate[1] - parentCoordinate[1];
        //控制点坐标 x等于 购物车x；y等于 addV的y
        controlP.x = endP.x;
        controlP.y = startP.y;

        //启动属性动画
        ObjectAnimator animator = ObjectAnimator.ofObject(img, "mPointF",
                new PointFTypeEvaluator(controlP), startP, endP);
        animator.setDuration(1000);
        animator.addListener(this);
        animator.start();
    }

    @Override
    public void onAnimationStart(Animator animation) {
    }

    @Override
    public void onAnimationEnd(Animator animation) {
        //动画结束后 父布局移除 img
        Object target = ((ObjectAnimator) animation).getTarget();
        idRalativelayout.removeView((View) target);
        //shopImg 开始一个放大动画
        Animation scaleAnim = AnimationUtils.loadAnimation(this, R.anim.shop_scale);
        cbMainCheckAll.startAnimation(scaleAnim);
    }

    @Override
    public void onAnimationCancel(Animator animation) {
    }

    @Override
    public void onAnimationRepeat(Animator animation) {
    }

    /**
     * 自定义估值器
     */
    public class PointFTypeEvaluator implements TypeEvaluator<PointF> {
        /**
         * 每个估值器对应一个属性动画，每个属性动画仅对应唯一一个控制点
         */
        PointF control;
        /**
         * 估值器返回值
         */
        PointF mPointF = new PointF();

        public PointFTypeEvaluator(PointF control) {
            this.control = control;
        }

        @Override
        public PointF evaluate(float fraction, PointF startValue, PointF endValue) {
            return getBezierPoint(startValue, endValue, control, fraction);
        }

        /**
         * 二次贝塞尔曲线公式
         *
         * @param start   开始的数据点
         * @param end     结束的数据点
         * @param control 控制点
         * @param t       float 0-1
         * @return 不同t对应的PointF
         */
        private PointF getBezierPoint(PointF start, PointF end, PointF control, float t) {
            mPointF.x = (1 - t) * (1 - t) * start.x + 2 * t * (1 - t) * control.x + t * t * end.x;
            mPointF.y = (1 - t) * (1 - t) * start.y + 2 * t * (1 - t) * control.y + t * t * end.y;
            return mPointF;
        }
    }
}
